/*
 * Copyright 2010-2011 Sanjay Dwivedi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.isdinvestments.ui.richgui.vaadin.common.fields;

import java.util.ArrayList;
import java.util.List;

import com.isdinvestments.cam.domain.model.Address;
import com.vaadin.data.Buffered;
import com.vaadin.data.Property;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;

public class AddressField extends CustomField {
	private static final long serialVersionUID = 8971083573902625347L;
	private Form addressForm;

	public AddressField() {
		this(null);
	}

	public AddressField(final Form parentForm) {
		if (parentForm != null) {
			addressForm = new EmbeddedForm(parentForm);
		} else {
			addressForm = new Form();
			VerticalLayout layout = new VerticalLayout();
			layout.setSpacing(true);
			
		}
		setCaption("Address");
        addressForm.setWriteThrough(false);
		setCompositionRoot(addressForm);
	}
	
    @Override
    public void setInternalValue(Object newValue) throws ReadOnlyException,
            ConversionException {
        // create the address if not given
        Address address = (newValue instanceof Address) ? (Address) newValue
                : new Address();

        super.setInternalValue(address);

        // set item data source and visible properties in a single operation to
        // avoid creating fields multiple times
        List<String> visibleProperties = new ArrayList<String>();
        visibleProperties.add("addressLine1");
        visibleProperties.add("addressLine2");
        visibleProperties.add("city");
        visibleProperties.add("state");
        visibleProperties.add("country");
        visibleProperties.add("zip");
        addressForm.setItemDataSource(new BeanItem<Address>(address), visibleProperties);
    }
	
    /**
     * commit changes of the address form
     */
    @Override
    public void commit() throws Buffered.SourceException, InvalidValueException {
        super.commit();
        addressForm.commit();
    }
    
    /**
     * discard changes of the address form
     */
    @Override
    public void discard() throws Buffered.SourceException {
        super.discard();
        addressForm.discard();
    }

	@Override
	public Class<?> getType() {
		return Address.class;
	}
}
